﻿using RimWorld;
using System.Reflection;
using UnityEngine;
using Verse;

namespace Miao.Stand
{
    public class Pawn_DrawTrackerSpecial : Pawn_DrawTracker
    {
        public StarPlatinumStand Stand
        {
            get
            {
                if(stand==null)
                {
                    stand = (pawn.GetStandHediff().stand) as StarPlatinumStand;
                }
                return stand;
            }
        }
        private StarPlatinumStand stand;
        private Pawn pawn;
        private bool theWorld
        {
            get
            {
                return Stand.TheWorld;
            }
        }
        public JitterHandler ReflectionJitter
        {
            get
            {
                if (jitterHandler == null)
                {
                    FieldInfo jtInfo = typeof(Pawn_DrawTracker).GetField("jitterer", BindingFlags.NonPublic | BindingFlags.Instance);
                    jitterHandler = jtInfo.GetValue(this) as JitterHandler;
                }
                return jitterHandler;
            }
        }
        private JitterHandler jitterHandler;
        new public Vector3 DrawPos
        {
            get
            {
                Vector3 tweenedPos;
                if (this.theWorld)
                {
                    tweenedPos = TheWorldDrawPos();
                }
                else
                {
                    tweener.PreDrawPosCalculation();
                    tweenedPos = tweener.TweenedPos;
                }
                tweenedPos += ReflectionJitter.CurrentOffset;
                tweenedPos += leaner.LeanOffset;
                tweenedPos += OffsetForcedByJob();
                tweenedPos.y = pawn.def.Altitude;
                return tweenedPos;
            }
        }
        private Vector3 OffsetForcedByJob()
        {
            if (pawn.jobs != null && pawn.jobs.curDriver != null)
            {
                return pawn.jobs.curDriver.ForcedBodyOffset;
            }
            return Vector3.zero;
        }
        public Vector3 TheWorldDrawPos()
        {
            float movePercent = MovedPercent();
            return pawn.pather.nextCell.ToVector3Shifted() * movePercent + pawn.Position.ToVector3Shifted() * (1f - movePercent) + PawnCollisionTweenerUtility.PawnCollisionPosOffsetFor(pawn);
        }
        private float MovedPercent()
        {
            if (!pawn.pather.Moving)
            {
                return 0f;
            }
            if (pawn.stances.FullBodyBusy)
            {
                return 0f;
            }
            if (pawn.pather.BuildingBlockingNextPathCell() != null)
            {
                return 0f;
            }
            if (pawn.pather.NextCellDoorToWaitForOrManuallyOpen() != null)
            {
                return 0f;
            }
            if (pawn.pather.WillCollideWithPawnOnNextPathCell())
            {
                return 0f;
            }
            return 1f - pawn.pather.nextCellCostLeft / pawn.pather.nextCellCostTotal;
        }
        public Pawn_DrawTrackerSpecial(Pawn pawn) : base(pawn)
        {
            this.pawn = pawn;
        }
    }
}
